﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using EasyStudy.Logic.Percentage;
using ComponentFactory.Krypton.Toolkit;
using EasyStudy.Logic;
using EasyStudy.Properties;

namespace EasyStudy.UI
{
    public partial class FrmTaskSummary : KryptonForm
    {
        private DataTable dt;

        public FrmTaskSummary()
        {
            InitializeComponent();

            int listViewWidth = this.listView.Size.Width;

            // Init listview control
            ColumnHeader chTitle = new ColumnHeader();
            chTitle.Text = "标题";
            chTitle.TextAlign = HorizontalAlignment.Left;
            chTitle.Width = listViewWidth * 2 / 3;

            ColumnHeader chPercentage = new ColumnHeader();
            chPercentage.Text = "进度";
            chPercentage.TextAlign = HorizontalAlignment.Center;
            chPercentage.Width = listViewWidth - chTitle.Width;

            this.listView.Columns.Add(chTitle);
            this.listView.Columns.Add(chPercentage);
            this.listView.SizeChanged += new EventHandler(listView_SizeChanged);

            loadAllTaskSummary();

            Icon = Resources.task1;
            msgBox = new KryptonTaskDialog();
        }

        void listView_SizeChanged(object sender, EventArgs e)
        {
            int listViewWidth = this.listView.Size.Width;

            // Init listview control
            ColumnHeader chTitle = this.listView.Columns[0];
            chTitle.Width = listViewWidth * 2 / 3;

            ColumnHeader chPercentage = this.listView.Columns[1];
            chPercentage.Width = listViewWidth - chTitle.Width;

            this.listView.Update();
        }

        public void loadDataSource(DataTable newTable)
        {
            DataView dv = new DataView(newTable);
            dv.AllowDelete = false;
            dv.AllowEdit = false;
            dv.AllowNew = false;
            dgv.DataSource = dv;
        }

        public void loadDataSource()
        {
            dt = EbbTaskDBMgr.GetAllTasks();
            loadDataSource(dt);
        }

        private void loadAllTaskSummary()
        {
            List<Percentage> percentages = PercentageDBMgr.GetAllProjectsPercentage();

            // 创建ListViewGroup
            List<ListViewGroup> lvgs = new List<ListViewGroup>();
            foreach (string typeName in EbbTaskDBMgr.getAllCategories())
            {
                lvgs.Add(new ListViewGroup(typeName));
            }
            listView.Groups.AddRange(lvgs.ToArray());

            //foreach (Percentage p in percentages)
            //{
            //    CtrlTaskSummary taskSummaryCtrl = new CtrlTaskSummary();
            //    taskSummaryCtrl.Dock = DockStyle.Top;
            //    taskSummaryCtrl.Subject = p.Subject;
            //    taskSummaryCtrl.Percentage = p.PercentageValue;
            //    taskSummaryCtrl.PercentageText = countPercent(p.PercentageValue, p.MaxPercentage);
            //    taskSummaryCtrl.MaxPercentage = p.MaxPercentage;
            //    this.kryptonPanel.Controls.Add(taskSummaryCtrl);
            //}

            this.listView.BeginUpdate();
            Font nfont = new System.Drawing.Font(this.Font.FontFamily, 10F);

            float percent = 0.0F;
            int listViewGroupIndex = -1;
            foreach (Percentage p in percentages)
            {
                ListViewItem lvi = new ListViewItem();

                percent = (float)p.PercentageValue / (float)p.MaxPercentage;
                if (percent <= 0.3)
                {
                    lvi.ForeColor = Color.FromArgb(217, 83, 79);
                }
                else if (percent >= 0.9)
                {
                    lvi.ForeColor = Color.FromArgb(51, 122, 183);
                }
                lvi.Font = nfont;
                lvi.Text = p.Subject;

                // 添加到对应的ListViewGroup
                for (int i = 0; i < lvgs.Count; ++i)
                {
                    if (lvgs[i].Header == p.Type) 
                    {
                        listViewGroupIndex = i;
                        break;
                    }
                }

                if (listViewGroupIndex != -1)
                {
                    lvi.Group = lvgs[listViewGroupIndex];
                }

                lvi.SubItems.Add(countPercent(p.PercentageValue, p.MaxPercentage));
                this.listView.Items.Add(lvi);
            }
            this.listView.EndUpdate();

            loadDataSource();

            dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dgv.MultiSelect = false;
            for (int i = 0; i < dt.Columns.Count; ++i)
            {
                this.dgv.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            }
            this.dgv.Columns[0].Visible = false;
            this.dgv.Columns[1].FillWeight = 15;
            this.dgv.Columns[2].FillWeight = 10;
            this.dgv.Columns[3].FillWeight = 15;
            this.dgv.Columns[4].FillWeight = 20;
            this.dgv.Columns[5].FillWeight = 15;
            this.dgv.Columns[6].FillWeight = 25;

            headerTask.Text = "您一共Log了" + dt.Rows.Count + "个艾宾浩斯任务!";
        }

        public static string countPercent(int value, int maxValue)
        {
            float fp1 = (float)value / (float)maxValue;
            int ip1 = (int)(fp1 * 100);

            return ip1.ToString() + "%";
        }

        private void rbData_CheckedChanged(object sender, EventArgs e)
        {
            if (rbData.Checked)
            {
                this.kryptonPanel.Visible = false;
                this.pnlOps.Visible = true;
                dgv.Visible = true;
            }
            else
            {
                this.kryptonPanel.Visible = true;
                this.pnlOps.Visible = false;
                dgv.Visible = false;
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (dgv.SelectedRows != null && dgv.SelectedRows.Count == 1)
            {
                EbbinghausTask task = new EbbinghausTask();

                DataGridViewRow row = dgv.SelectedRows[0];
                task.ID = row.Cells[0].Value.ToString();
                task.Type = row.Cells[1].Value.ToString();
                task.Subject = row.Cells[2].Value.ToString();
                task.Comment = row.Cells[3].Value.ToString();

                FrmUpdateEbbTask frmUpdateTask = new FrmUpdateEbbTask(task);
                DialogResult diaRes = frmUpdateTask.ShowDialog(this);
                if (diaRes == System.Windows.Forms.DialogResult.OK)
                {
                    loadDataSource();
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dgv.SelectedRows != null && dgv.SelectedRows.Count == 1)
            {
                DataGridViewRow row = dgv.SelectedRows[0];
                string id = row.Cells[0].Value.ToString();

                msgBox.Content = "学习是一项需要长期坚持的事情，您已经确认好了要删除该任务了吗？";
                msgBox.WindowTitle = "删除任务";
                msgBox.CommonButtons = TaskDialogButtons.OK | TaskDialogButtons.Cancel;

                DialogResult dres = msgBox.ShowDialog(this);
                if (dres == System.Windows.Forms.DialogResult.OK)
                {
                    EbbTaskDBMgr.DeleteATask(id);
                    loadDataSource();
                }
            }
        }

        private void cmdCancelQuery_Execute(object sender, EventArgs e)
        {
            loadAllTaskSummary();
        }

        private void btnQuery_Click_1(object sender, EventArgs e)
        {
            FrmQueryTask frmQuery = new FrmQueryTask();
            DialogResult diaRes = frmQuery.ShowDialog(this);

            if (diaRes == System.Windows.Forms.DialogResult.OK)
            {
                loadDataSource(frmQuery.DataSource);
            }
        }
    }
}
